'use strict';

define(['angular', 'moment', 'common/utils/blocking-rest'], function (angular, moment) {
	var module = angular.module('dataService', ['blockingRestService']);

	module.factory('dataService', ['blockingRestService', function (mpjRest) {

		return {
			getStoreInfo: getStoreInfo,
			getMemberBind: getMemberBind,
			getOpenId: getOpenId,
			getMemberInfo: getMemberInfo,
			getAccountData: getAccountData,
			getPointsData: getPointsData,
			getMemberOneInfo: getMemberOneInfo,
			getMemberOrders: getMemberOrders,
			getOrderInfo: getOrderInfo,
			sendMemberBindSmsCode: sendMemberBindSmsCode,
			bindMember: bindMember,
			generateMonthData: generateMonthData,
			getOneAccountTx: getOneAccountTx,
			getOnePointsTx: getOnePointsTx,
			getBindList: getBindList,
			unBindMember: unBindMember,
			changePassword: changePassword,
			getMemberPayReq: getMemberPayReq,
			updatePayReq: updatePayReq,
			createNewCode: createNewCode,
			getVerifyCodePayment: getVerifyCodePayment,
			sendMemberRegisterSmsCode: sendMemberRegisterSmsCode,
			getStoreConfig: getStoreConfig,
			registerMember: registerMember
		};
		function registerMember(storeId, member, cb) {
			var url = '/stores/' + storeId + '/member-register';
			mpjRest.post(url, member, cb);
		}

		function getStoreConfig(storeId, type, cb) {
			var url = '/stores/' + storeId + '/config?key=' + type;
			mpjRest.get(url, cb);
		}

		function sendMemberRegisterSmsCode(storeId, data, cb) {
			var url = '/stores/' + storeId + '/member-register/send-sms';
			mpjRest.post(url, data, cb);
		}

		function getVerifyCodePayment(storeId, memberId, code, cb) {
			var url = '/stores/' + storeId + '/member-pay/' + memberId + '/payment?verifyCode=' + code;
			mpjRest.get(url, cb);
		}

		function createNewCode(storeId, memberId, cb) {
			var url = '/stores/' + storeId + '/member-pay/' + memberId + '/verifyCode';
			mpjRest.post(url, null, cb);
		}

		function updatePayReq(data, cb) {
			var url = '/api/member/' + data.payer_id + '/payreq/' + data.id + "?openToken=" + data.oper_token;
			mpjRest.put(url, data, cb);
		}

		function getMemberPayReq(storeId, memberId, cb) {
			var url = '/api/member/' + memberId + '/payreq?storeId=' + storeId;
			mpjRest.get(url, cb);
		}

		function changePassword(pwd, memberId, cb) {
			var url = '/api/member/' + memberId + '/changepassword';
			mpjRest.put(url, {pwd: pwd}, cb);
		}

		function unBindMember(bindId, cb) {
			mpjRest.delete("/api/bindMember/" + bindId, null, cb);
		}

		function getBindList(memberId, storeId, cb) {
			mpjRest.get("/api/stores/" + storeId + "/member/" + memberId + "/bindlist", cb);
		}

		function getPointsData(memberId, type, month, cb) {
			mpjRest.get("/api/member/" + memberId + "/points?type=" + type + '&month=' + month, cb);
		}

		function getStoreInfo(storeId, cb) {
			mpjRest.get('/stores/' + storeId, cb);
		}

		function getMemberBind(storeId, openId, sns, cb) {
			mpjRest.get('/api/stores/' + storeId + '/bindMember?openId=' + openId + '&sns=' + sns, cb, {ownError: true});
		}

		function getOpenId(storeId, code, sns, cb) {
			mpjRest.get('/api/stores/' + storeId + '/wechat/openid?code=' + code, cb);
		}


		function getMemberInfo(id, cb) {
			mpjRest.get('/api/member/' + id, cb);
		}

		function getAccountData(memberId, type, month, cb) {
			mpjRest.get("/api/member/" + memberId + "/account?type=" + type + '&month=' + month, cb);
		}

		function getMemberOneInfo(memberId, cb) {
			mpjRest.get('/api/member/' + memberId + '/one', cb);
		}

		function getMemberOrders(memberId, month, cb) {
			mpjRest.get('/api/member/' + memberId + '/orders?month=' + month, cb);
		}

		function getOrderInfo(orderId, cb) {
			mpjRest.get('/api/orders/' + orderId, cb);
		}

		function sendMemberBindSmsCode(storeId, data, cb) {
			var url = '/stores/' + storeId + '/member-bind/send-sms';
			mpjRest.post(url, data, cb);
		}

		function bindMember(storeId, data, cb) {
			var url = '/stores/' + storeId + '/member-bind/bind';
			mpjRest.post(url, data, cb);
		}

		function generateMonthData(month, list, listName, timeField) {
			var monthData = [];
			for (var i = 0; i < 3; i++) {
				var data = {};
				data[listName] = [];
				data.count = {count: 0};
				data.count.month = moment(month).subtract(i, 'month').valueOf();
				monthData.push(data);
			}
			var startDate = moment(month).startOf("month");
			angular.forEach(list, function (tx) {
				var months = Math.floor(startDate.diff(moment(tx[timeField]), "months", true)) + 1;
				monthData[months][listName].push(tx);
				monthData[months].count.count++;
			});
			var ret = [];
			for (var i in monthData) {
				if (monthData[i].count.count > 0) {
					ret.push(monthData[i]);
				}
			}
			return ret;
		}

		function getOneAccountTx(txId, cb) {
			mpjRest.get('/api/member/account/tx/' + txId, cb);
		}

		function getOnePointsTx(txId, cb) {
			mpjRest.get('/api/member/points/tx/' + txId, cb);
		}
	}]);
});
